package com.wanxi.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanxi.dao.ProductDao;
import com.wanxi.dao.ProductTypeDao;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.Product;
import com.wanxi.model.ProductType;
import com.wanxi.service.ProductTypeService;
import com.wanxi.util.CreateOnlyID;
import com.wanxi.util.JsonCopy;
import com.wanxi.vo.ProductTypeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class ProductTypeServiceImpl implements ProductTypeService {
    @Autowired
    ProductTypeDao productTypeDao ;
    @Autowired
    CreateOnlyID createOnlyId;
    @Autowired
    ProductDao productDao;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Override
    public ResultDTO findAllType() {
        QueryWrapper<ProductType> queryWrapper=new QueryWrapper<>();
        List<ProductType> list = productTypeDao.selectList(queryWrapper);
        if (list.size() > 0) {
            return ResultDTO.getDTO(list.size(), list);
        }
        return ResultDTO.builder().data(list).msg("no data").count(0).code(1).build();
    }

    @Override
    public ResultDTO findProductTypeById(ProductTypeVO productTypeVO) {
        ProductType productType = JsonCopy.copy(productTypeVO, ProductType.class);
        ProductType pt = productTypeDao.selectById(productType.getId());
        if (pt != null) {
            return ResultDTO.getDTO(1, pt);
        }
        return ResultDTO.builder().count(0).msg("error").code(1).build();
    }

    @Override
    public ResultDTO updateEnable(ProductTypeVO productTypeVO) {
        ProductType productType = JsonCopy.copy(productTypeVO, ProductType.class);
        productType.setUpdateTime(new Date());
        int i = productTypeDao.updateById(productType);
        if (i == 1) {
            stringRedisTemplate.delete("productType");
            return ResultDTO.getDTO(i, productTypeVO);
        }
        return ResultDTO.builder().data(productTypeVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO addProductType(ProductTypeVO productTypeVO) {
        ProductType productType = JsonCopy.copy(productTypeVO, ProductType.class);
        productType.setId(createOnlyId.createId());
        productType.setUpdateTime(new Date());
        productType.setCreateTime(new Date());
        int i = productTypeDao.insert(productType);
        if (i == 1) {

            stringRedisTemplate.delete("productType");
            return ResultDTO.getDTO(i, productTypeVO);
        }
        return ResultDTO.builder().data(productTypeVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO deleteProductType(ProductTypeVO productTypeVO) {
        ProductType productType = JsonCopy.copy(productTypeVO, ProductType.class);
        Product product=Product.builder()
                .productType(ProductType.builder()
                        .id(productType.getId())
                        .build())
                .build();
        if(productDao.findProduct(product).size()<1){
            int i = productTypeDao.deleteById(productType);
            if (i == 1) {
                stringRedisTemplate.delete("productType");
                return ResultDTO.getDTO(i, productTypeVO);
            }
        }
        return ResultDTO.builder().data(productTypeVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO updateProductType(ProductTypeVO productTypeVO) {
        ProductType productType = JsonCopy.copy(productTypeVO, ProductType.class);
        productType.setUpdateTime(new Date());
        int i = productTypeDao.updateById(productType);
        if (i == 1) {
            stringRedisTemplate.delete("productType");
            return ResultDTO.getDTO(i, productTypeVO);
        }
        return ResultDTO.builder().data(productTypeVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO findType() {
        Long len = stringRedisTemplate.opsForList().size("productType");
//        判断redis中是否有数据
        if (len != 0) {
//            返回redis中的数据,通过fastJson转换
            List<String> redisProductType = stringRedisTemplate.opsForList()
                    .range("productType",0,len);
            List<ProductType> list = JSON.parseObject(redisProductType.toString(), new TypeReference<List<ProductType>>() {
            });
            return ResultDTO.getDTO(list.size(), list);
        }
//        没有则从数据库中找数据
        List<ProductType> list = productTypeDao.selectList(null);
        if (list.size() > 0) {
            Iterator<ProductType> iterator = list.iterator();
            while (iterator.hasNext()) {
                ProductType pt = iterator.next();
                if (pt.getEnable() == 0) {
                    iterator.remove();
                }
                else {
//                    将查到的数据放入redis
                    stringRedisTemplate.expire("productType",300, TimeUnit.SECONDS);
                    stringRedisTemplate.opsForList().rightPush("productType", JSON.toJSONString(pt));
                }
            }
            return ResultDTO.getDTO(list.size(), list);
        }
        return ResultDTO.builder().data(list).msg("no data").count(0).code(1).build();
    }
}
